package com.td.erhome.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Vibrator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.td.erhome.R;
import com.td.erhome.utils.ImageLoader;

import java.util.List;

import static android.content.Context.VIBRATOR_SERVICE;


/**
 * Created by Administrator on 2016/10/21 0021.
 */
public class WritrBookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{
    private Activity mContext;
    public static final int TYPE_HEADER = 0;  //Header
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    //HeaderView, FooterView
    private View mHeaderView;
    private View mFooterView;
    public List<String> mPaths;

    public boolean isShow = false;

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public WritrBookAdapter(Activity context,List<String> paths) {
        mContext = context;
        mPaths = paths;
    }

    public void addAllData(List<String> paths){
        mPaths.addAll(paths);
        notifyDataSetChanged();
    }

    //HeaderView和FooterView的get和set函数
    public View getHeaderView() {
        return mHeaderView;
    }
    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }
    public View getFooterView() {
        return mFooterView;
    }
    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    /** 重写这个方法，很重要，是加入Header和Footer的关键，我们通过判断item的类型，从而绑定不同的view    * */
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null){
            return TYPE_NORMAL;
        }
        if (mHeaderView != null && position == 0){
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        if (mFooterView != null && position == getItemCount()-1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(mHeaderView != null && viewType == TYPE_HEADER) {
            return new ListHolder(mHeaderView);
        }
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new ListHolder(mFooterView);
        }else{
            View  inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.write_book_list, null);
            return new ListHolder(inflate);
        }

    }

    /**
     * 绑定ViewHolder
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder instanceof ListHolder) {
                //如果有header则 从position-1开始，因为position==0已经被header占用了

                   ((ListHolder) holder).mRemoveImg.setVisibility(View.VISIBLE);
                   ((ListHolder) holder).mRemoveRe.setOnClickListener(this);
                   ((ListHolder) holder).mRemoveRe.setTag(position);

                  ImageLoader.loadImaged(mContext, mPaths.get(position), R.mipmap.td_moreng, R.mipmap.td_moreng, ((ListHolder) holder).mAddImg);
                return;
            }

        }else if(getItemViewType(position) == TYPE_HEADER){
            return;
        }else if (getItemViewType(position) == TYPE_FOOTER){

            return;
        }
    }



    //define interface
    public  interface OnRecyclerViewItemClickListener {
        void onItemClick(int point);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick((int)v.getTag());
        }
    }
    /**
     * Count 大小
     * @return
     */
    @Override
    public int getItemCount() {
        if(mHeaderView == null && mFooterView == null){
            return mPaths.size();
        }else if(mHeaderView == null && mFooterView != null){
            return mPaths.size()+1;
        }else if (mHeaderView != null && mFooterView == null){
           return mPaths.size()+1;
        }else {
         return mPaths.size() + 2;
        }
    }

    class ListHolder extends RecyclerView.ViewHolder{

        public RelativeLayout mRemoveRe;
        public ImageView mRemoveImg;
        public  ImageView mAddImg;

        public ListHolder(View itemView) {
            super(itemView);

            if (itemView == mHeaderView){
                return;
            }
            if (itemView == mFooterView){
                return;
            }
            mAddImg = (ImageView) itemView.findViewById(R.id.list_add_img);
            mRemoveImg = (ImageView) itemView.findViewById(R.id.img_remove);
            mRemoveRe = (RelativeLayout) itemView.findViewById(R.id.re_remove);

        }

        /**
         * 手机震动
         */
        public void getVib(Context context){
            Vibrator vibrator = (Vibrator)context.getSystemService(VIBRATOR_SERVICE);
            boolean b = vibrator.hasVibrator();
            if (!b){
                return;
            }
            vibrator.vibrate(50);
        }

    }
}
